package ReSofting.it.GoldDirector;

import java.io.IOException;

import ReSofting.it.GoldDirector.Db.DataDbManager;
import ReSofting.it.GoldDirector.Db.DatabaseHelper;
import ReSofting.it.GoldDirector.Helpers.InputBoxBool;
import ReSofting.it.GoldDirector.Helpers.ItemsLoader;
import ReSofting.it.GoldDirector.Helpers.resVarie;
import ReSofting.it.GoldDirector.Interfaces.IInputBoxBool;
import ReSofting.it.GoldDirector.Model.GlobalDatas;
import ReSofting.it.GoldDirector.Model.myNetSettings;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SettingsActivity extends Activity 
implements IInputBoxBool
{

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.settings);
	    	    
	    setButtonsHandlers();
	    
	    setNetSettings(null);
	}
	
	
	
	 //Menues*********************************************************
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {	
    	return resVarie.onCreateOptionsMenu(menu, this);
    }//fM


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {	
    	return resVarie.handlerMenu(item, this);	 
    } //fm
    
    private void setButtonsHandlers() {
		
    	 Button startBtn = (Button)  findViewById(R.id.settingsBut_TestDb);	     
		 startBtn.setOnClickListener(new OnClickListener() {
	            public void onClick(View view) 
	            {  testDB();  }});
		 
		 Button getItemsBtn = (Button)  findViewById(R.id.settingsBut_GetItems);	     
		 getItemsBtn.setOnClickListener(new OnClickListener() {
	            public void onClick(View view) 
	            {    getItemsOnDb(); 	 }});
		 
		 
		 Button saveSettBtn = (Button)  findViewById(R.id.settingsBut_SaveSettings);	     
		 saveSettBtn.setOnClickListener(new OnClickListener() {
	            public void onClick(View view) 
	            {    saveSettings(); 	 }});
		 
		 Button delDbeBtn = (Button)  findViewById(R.id.settingsBut_DelDb);	     
		 delDbeBtn.setOnClickListener(new OnClickListener() {
	            public void onClick(View view) 
	            {    deleteDb(); 	 }});
		 
		 
		 
    }
    

    
    
    //***************************
    public void testDB()
    {
    	 
    	DatabaseHelper myDbHelper = null;
  
         myDbHelper = new DatabaseHelper(this);
         try {
			myDbHelper.createDataBase();
			
			  Boolean ok = myDbHelper.checkDataBase();
		      Log.d( "Esito check db " , ok + "  " );
		         
		      Toast.makeText(getApplicationContext(), "Esito check db : " + ok + "  " , Toast.LENGTH_SHORT).show();
		         
		      
		      String info = myDbHelper.getInfoTabelleDatiDb();
		      
		      Toast.makeText(getApplicationContext(), "Info db : " + info + "  " , Toast.LENGTH_LONG).show();
		       
		      
		      myDbHelper.close();
		      myDbHelper = null;
		         
		} catch (IOException e) 
		{
			Toast.makeText(getApplicationContext(), "Errore crea db \r\n" + e.getMessage()+ "  " , Toast.LENGTH_SHORT).show();
			Log.d("Errore crea db", e.getMessage());
		}
         
		    	
    }//fM
    
    
    public void setNetSettings(myNetSettings pNetSet )
    {
    	
    	EditText host = (EditText)  findViewById(R.id.settingsTxt_IPHOST);	
    	EditText port = (EditText)  findViewById(R.id.settingsTxt_HOSTPORT);	
    	//EditText startBtn = (EditText)  findViewById(R.id.settingsBut_TestDb);	
    	
    	if(pNetSet == null)
    	{
    		DataDbManager dbMan = new  DataDbManager(this);
    		pNetSet = dbMan.getNetSettingsFromDb();
    		dbMan.CloseDb();
   		 	dbMan = null;
    	}
		
		if(pNetSet != null)
		{
			host.setText(pNetSet.HostIp);
			port.setText(pNetSet.Port);
		}
		
		GlobalDatas.NetSettings = new myNetSettings(pNetSet.HostIp, pNetSet.Port,pNetSet.ServiceCore, pNetSet.ServiceLogin );
		 
		
    }
    
    public void saveSettings()
    {
    	
    	EditText host = (EditText)  findViewById(R.id.settingsTxt_IPHOST);	
    	EditText port = (EditText)  findViewById(R.id.settingsTxt_HOSTPORT);	
    	//EditText startBtn = (EditText)  findViewById(R.id.settingsBut_TestDb);	
    	
    	DataDbManager dbMan = new  DataDbManager(this);
		 
    	
    	myNetSettings set = new myNetSettings(
    			host.getText().toString() ,port.getText().toString(),
				"", "");
    	
		dbMan.addNetSettingsInDb(set,true);
		 
		 dbMan.CloseDb();
		 dbMan = null;
		 
		 Log.v("In saveSettings", "Dati salvati");
		 resVarie.Alert2("Messaggio", "Dati Salvati", this);
		
    }
    
    public void getItemsOnDb()
    {
    	ItemsLoader itM = new ItemsLoader(this);
    	
    	itM.getItems();
    	
    	itM.CloseDb();
    	itM = null;
    }

    private void deleteDb()
    {    	
    	InputBoxBool box = new InputBoxBool(this);
    	box.showInputBox("Dati", "Cancellare il Db ?", "", this, "CancellaDb", "");
    	
    }


	public void onInputBoxBoolCallComplete(Boolean pResponse, Object pTag,
			String pToken) {
		
		if(pToken == "CancellaDb")
		{
			Log.v("In onInputBoxBoolCallComplete", pResponse.toString());
			
			if(pResponse == true)
				this.deleteDatabase(DatabaseHelper.DB_NAME);
			
			Toast.makeText(this, "Db deleted", Toast.LENGTH_LONG);
		}
		
	}
    

}//fC
